package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.Impl.CompanyDaoImpl;
import com.ysd.entity.Company;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminCompanyServlet extends BaseServlet {
	/**
	 * localhost:8080/huaduoyoupin/adminCompany?reqName=getCompany
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getCompany(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CompanyDaoImpl cDao=new CompanyDaoImpl();
		Company cList=cDao.getCompany();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		String res=gson.toJson(cList);
		out.print(res);
		
		out.flush();
		out.close();
	}
    /*
     * localhost:8080/huaduoyoupin/adminCompany?reqName=getCompanyById
     */
	public void getCompanyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CompanyDaoImpl cDao=new CompanyDaoImpl();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		int cId=Integer.parseInt(request.getParameter("id"));
		Company c=cDao.getCompanyById(cId);
		
		Gson gson=new Gson();
		String res=gson.toJson(c);
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/huaduoyoupin/adminCompany?reqName=updateCompanyById
	 * 修改公司内容
	 */
	public void updateCompanyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CompanyDaoImpl cDao=new CompanyDaoImpl();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		 int id=0;
			if(request.getParameter("id")!=null && !request.getParameter("id").equals("")) {
				id=Integer.parseInt(request.getParameter("id"));
			}
		String name=request.getParameter("name");
		String webSite=request.getParameter("webSite");
		String introduction=request.getParameter("introduction");
		String phone=request.getParameter("phone");
		String email=request.getParameter("email");
		String address=request.getParameter("address");
		String culture=request.getParameter("culture");
		String logo=request.getParameter("logo");
        Company c=new Company(name, webSite, introduction, phone, email, address, culture, logo, id);
        int n=cDao.updateCompanyById(c);
        if(n>0) {
        	out.print(Result.toClient(true, "修改成功",""));
        }else {
        	out.print(Result.toClient(false, "修改失败",""));
        }
		
		
		out.flush();
		out.close();
	}
}
